package net.jiug.common.runner;

import net.jiug.common.config.ConfigEmail;
import net.jiug.common.utils.EmailUtils;
import net.jiug.email.model.Email;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 初始化邮箱会话
 */
@Component
public class InitEmailAccount implements CommandLineRunner {

    @Resource
    private EmailUtils emailUtils;
    @Resource
    private ConfigEmail configEmail;

    @Override
    public void run(String... args) throws Exception {
        List<Email> emailList = configEmail.getEmailList();
        if(emailList.size()==0){
           throw new RuntimeException("未配置邮箱");
        }
        emailUtils.updateSession(
                emailList.get(0).getHost(),
                emailList.get(0).getPort(),
                emailList.get(0).getAccount(),
                emailList.get(0).getSecretKey(),
                configEmail.getNick());
    }

}
